package com.fashiontextile.common.handler;

import com.fashiontextile.common.entity.R;
import com.fashiontextile.common.exception.AuthenticationException;
import com.fashiontextile.common.exception.ForbiddenException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器，统一处理鉴权不通过等错误
 *
 * @author brooke_zb
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理未登录错误 401
     */
    @ExceptionHandler(AuthenticationException.class)
    public ResponseEntity<R<String>> handleAuthenticationException(Exception ex) {
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(R.fail(ex.getMessage()));
    }

    /**
     * 处理未授权错误 403
     */
    @ExceptionHandler(ForbiddenException.class)
    public ResponseEntity<R<String>> handleForbiddenException(Exception ex) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(R.fail(ex.getMessage()));
    }
}
